package pers.lt.admin.controller;


import net.sf.json.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.lt.admin.services.OfficesServicesImp;
import pers.lt.hibernatebeans.OfficesInfoEntity;
import pers.lt.tools.StringFacs;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * Created by liting on 2016/7/5.
 */
@Controller
@RequestMapping(value = "/admin/offices")
public class OfficesController {
    @Resource
    private OfficesServicesImp officesServicesImp;
    //跳转权限列表
    @RequestMapping(value = "/index.html")
    public String  officesIndex(HttpServletRequest request, HttpServletResponse response) {
        return "/admin/officeslist";
    }
    //跳转到新增页面
    @RequestMapping(value = "/new.html")
    public String  officesAddindex(OfficesInfoEntity officesInfoEntity, HttpServletRequest request, HttpServletResponse response) {
        //officesServicesImp.userSave(userInfoEntity);
        //更新记录
        if(!StringFacs.isEmpty(officesInfoEntity.getId())) request.setAttribute("cofficesinfo",officesServicesImp.selectOffices(officesInfoEntity));
        if(!StringFacs.isEmpty(officesInfoEntity.getPid())){
            //查出父级对象
            officesInfoEntity.setId(officesInfoEntity.getPid());
            request.setAttribute("pofficesinfo",officesServicesImp.selectOffices(officesInfoEntity));
        }
        return "/admin/offices";
    }
    //新增权限
    @RequestMapping(value = "/form.html")
    @ResponseBody
    public OfficesInfoEntity  officesform(OfficesInfoEntity officesInfoEntity,HttpServletRequest request, HttpServletResponse response) {
        return officesServicesImp.addOffices(officesInfoEntity);
    }
    //查询权限数据
    @RequestMapping(value = "/list.html")
    public void  officeslist(OfficesInfoEntity roleInfoEntity ,HttpServletRequest request, HttpServletResponse response) {
        List repage = officesServicesImp.officesList(roleInfoEntity);
        JSONArray jsonArray = JSONArray.fromObject(repage);
        String json = jsonArray.toString();
        // String ls_result = "{\"total\":"+li_total+",\"rows\":"+json+"}";
        PrintWriter out = null;
        response.setCharacterEncoding("utf-8");
        try {
            out = response.getWriter();
            out.write(json);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            out.flush();
            out.close();
        }
    }
    //组织easyui 下拉树 格式要求的数据
    @RequestMapping(value = "/comotreelist.html")
    public void  officeslistTree(OfficesInfoEntity roleInfoEntity ,HttpServletRequest request, HttpServletResponse response) {
        List repage = officesServicesImp.officesListTree(roleInfoEntity);
        JSONArray jsonArray = JSONArray.fromObject(repage);
        String json = jsonArray.toString();
        // String ls_result = "{\"total\":"+li_total+",\"rows\":"+json+"}";
        PrintWriter out = null;
        response.setCharacterEncoding("utf-8");
        try {
            out = response.getWriter();
            out.write(json);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            out.flush();
            out.close();
        }
    }
    //跳转用户列表
    @RequestMapping(value = "/delete.html")
    public void  officesDelete(OfficesInfoEntity officesInfoEntity,HttpServletRequest request, HttpServletResponse response) {
        //adminServices.userSave(userInfoEntity);
        officesServicesImp.officesDelete(officesInfoEntity);
    }
}
